package com.example.ex2.controller;

import com.example.ex2.common.CommonResponse;
import com.example.ex2.domain.Info;
import com.example.ex2.domain.Item;
import com.example.ex2.domain.NoReturnedItem;
import com.example.ex2.domain.Returninfo;
import com.example.ex2.persistence.BorrowinfoMapper;
import com.example.ex2.service.BorrowInfoService;
import com.example.ex2.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class BorrowInfoController {
    @Autowired
    private BorrowInfoService borrowInfoService;

    @Autowired
    private ItemService itemService;

    @PostMapping("/borrowInfo")
    public CommonResponse<Returninfo> addItem(@RequestParam("user_id") int user_id,
                                              @RequestParam("item_id") int item_id){
        System.out.println(user_id);
        String link = "http://localhost:8080/borrowInfo";
        Info info = new Info();
        info.setUser_id(user_id);
        info.setItem_id(item_id);
        if(borrowInfoService.addBorrowInfo(info))
            return CommonResponse.createForSuccess("新增成功", link);
        else
            return CommonResponse.createForError("新增失败",link);
    }
    @DeleteMapping(value = "/borrowInfo/{id}")
    public CommonResponse<Info> deleteItem(@PathVariable("id") int id){
        String link = "http://localhost:8080/borrowInfo/" + Integer.toString(id);
        if(borrowInfoService.deleteBorrowInfoByID(id))
            return CommonResponse.createForSuccess("删除成功",link);
        else
            return CommonResponse.createForError("删除失败",link);
    }
    @GetMapping("/borrowInfo")
    public CommonResponse<List<Info>> getAllBorrowInfo(){
        String link = "http://localhost:8080/borrowInfo";
        List<Info> itemList = borrowInfoService.selectAllBorrowInfo();
        if(itemList.size() > 0)
            return CommonResponse.createForSuccess("查询成功", itemList, link);
        else
            return CommonResponse.createForError("查询不到相关数据",link);
    }

    @PutMapping("/borrowInfo")
    public CommonResponse<Info> updateBorrowInfo(@RequestBody Info info){
        String link = "http://localhost:8080/borrowInfo";
        if(borrowInfoService.updateItemByBorrowInfo(info))
            return CommonResponse.createForSuccess("更新成功", link);
        else
            return CommonResponse.createForError("更新失败",link);
    }

    @GetMapping("/borrowInto/{user_id}")
    public CommonResponse<List<NoReturnedItem>> getNoReturnedItem(@PathVariable("user_id")int user_id){
        String link = "http://localhost:8080/borrowInfo/" + user_id;
        List<Info> noReturnedInfoList = borrowInfoService.selectNoReturnedBorrowInfoByUserId(user_id);
//        System.out.println(noReturnedInfoList);
        if (noReturnedInfoList.size() == 0){
            return CommonResponse.createForSuccess("当前用户没有未归还物品",link);
        }else {
            List<NoReturnedItem> noReturnedItemList = new ArrayList<>();
            for(int i = 0; i < noReturnedInfoList.size(); i++){
                Info info = noReturnedInfoList.get(i);
                NoReturnedItem noReturnedItem = new NoReturnedItem();
                noReturnedItem.setItem_id(info.getItem_id());
                noReturnedItem.setItem_name(itemService.selectNameByItemId(info.getItem_id()));
                noReturnedItem.setBorrow_time(info.getBorrow_time());
                noReturnedItem.setInfo_id(info.getId());
                noReturnedItemList.add(noReturnedItem);
            }
            return CommonResponse.createForSuccess("查询成功", noReturnedItemList,link);
        }
    }
}
